<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="活动商家" prop="eventMerchant.id"
                :rules="[
                  {required: true, message:'活动商家不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择活动商家"
            labelName = 'name'
            labelValue = 'id'
            :value = "inputForm.eventMerchant.id"
            :limit="1"
            @getValue='(value) => {inputForm.eventMerchant.id=value}'
            :columns="[
            {
              prop: 'id',
              label: '主键'
            },
            {
              prop: 'activityRole',
              label: '活动角色（0：平台，1：房产中介，2：楼盘，3：4S店，4：汽贸店，5：招聘企业）'
            },
            {
              prop: 'name',
              label: '（门店/活动）名称'
            },
            {
              prop: 'picture',
              label: '（门店/活动）图片'
            },
            {
              prop: 'address',
              label: '（门店/活动）地址'
            }
            ]"
            :searchs="[
            {
              prop: 'activityRole',
              label: '活动角色（0：平台，1：房产中介，2：楼盘，3：4S店，4：汽贸店，5：招聘企业）'
            },
            {
              prop: 'name',
              label: '（门店/活动）名称'
            },
            {
              prop: 'address',
              label: '（门店/活动）地址'
            }
            ]"
            dataListUrl="/common_eventmerchants/commonEventMerchants/list"
            entityBeanName="commonEventMerchants"
            queryEntityUrl="/common_eventmerchants/commonEventMerchants/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="礼品" prop="gift.id"
                :rules="[
                  {required: true, message:'礼品不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择礼品"
            labelName = 'giftName'
            labelValue = 'id'
            :value = "inputForm.gift.id"
            :limit="1"
            @getValue='(value) => {inputForm.gift.id=value}'
            :columns="[
            {
              prop: 'giftName',
              label: '礼品名称'
            },
            {
              prop: 'giftPicture',
              label: '礼品图片'
            },
            {
              prop: 'totalInventory',
              label: '总库存'
            },
            {
              prop: 'stock',
              label: '库存'
            }
            ]"
            :searchs="[
            {
              prop: 'giftName',
              label: '礼品名称'
            },
            {
              prop: 'stock',
              label: '库存'
            }
            ]"
            dataListUrl="/commoneventgifts/commonEventGifts/list"
            entityBeanName="commonEventGifts"
            queryEntityUrl="/commoneventgifts/commonEventGifts/queryById">
          </GridSelect>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="总库存" prop="totalInventory"
                :rules="[
                  {required: true, message:'总库存不能为空', trigger:'blur'},
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.totalInventory" placeholder="请填写总库存"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="库存" prop="stock"
                :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.stock" placeholder="请填写库存"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="库存预警值" prop="inventoryAlertValue"
                :rules="[
                  {validator: validator.isIntGtZero, trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.inventoryAlertValue" placeholder="请填写库存预警值"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="领取方式" prop="collectionMethod"
                :rules="[]">
              <el-select
                  v-model="collectionMethodNew"
                  placeholder="请选择领取方式"
                  style="width: 100%"
                  multiple
                  clearable
                >
                  <el-option label="线下领取" value="1"></el-option>
                  <el-option label="快递邮寄" value="2"></el-option>
                </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import GridSelect from '@/components/gridSelect'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          eventMerchant: {
            id: ''
          },
          gift: {
            id: ''
          },
          totalInventory: '',
          stock: '',
          inventoryAlertValue: '',
          collectionMethod: '',
          remarks: ''
        }
      }
    },
    components: {
      GridSelect
    },
    computed:{
      collectionMethodNew: {
        get() {
            return this.inputForm.collectionMethod.split(',').filter(item=>item) 
        },
        set(val) {
            this.inputForm.collectionMethod = val.join(',')
        }
      },
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建活动商家礼品信息`
        } else if (method === 'edit') {
          this.title = '修改活动商家礼品信息'
        } else if (method === 'view') {
          this.title = '查看活动商家礼品信息'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/commoneventmerchantgift/commonEventMerchantGift/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.commonEventMerchantGift)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/commoneventmerchantgift/commonEventMerchantGift/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>